package com.ruoyi.hxy.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.google.gson.Gson;
import com.ruoyi.hxy.constant.HxyConstant;
import com.ruoyi.hxy.dto.goods.HxyGoodsResultResDto;
import com.ruoyi.hxy.dto.goods.HxyGoodsSkuResDto;
import com.ruoyi.hxy.dto.order.HxyOrderResultResDto;
import com.ruoyi.hxy.service.IHxyGoodsService;
import com.ruoyi.xp.supply.dto.SupplyWarehouseGoodsDto;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author chendx
 */
@Service
public class HxyGoodsServiceImpl implements IHxyGoodsService {

    @Override
    public List<SupplyWarehouseGoodsDto> getGoodsSkuList() throws IOException {

        HxyGoodsResultResDto hxyGoodsResultResDto = getHxyWareGoodsSku();
        List<SupplyWarehouseGoodsDto> supplyWarehouseGoodsDtoList = new ArrayList<>();;
        if (HxyConstant.ORDER_RESULT_SUCCESS.equals(hxyGoodsResultResDto.getResult())
                && ObjectUtil.isNotNull(hxyGoodsResultResDto.getHxyGoodsDataResDto())
                && CollectionUtil.isNotEmpty(hxyGoodsResultResDto.getHxyGoodsDataResDto().getHxyGoodsSkuResDtoList())) {
            List<HxyGoodsSkuResDto> hxyGoodsSkuResDtoList = hxyGoodsResultResDto.getHxyGoodsDataResDto().getHxyGoodsSkuResDtoList();
            for (HxyGoodsSkuResDto hxyGoodsSkuResDto : hxyGoodsSkuResDtoList) {
                SupplyWarehouseGoodsDto supplyWarehouseGoodsDto = new SupplyWarehouseGoodsDto();
                supplyWarehouseGoodsDto.setSupplyWarehouseGoodsId(hxyGoodsSkuResDto.getGoodsSkuWarehouseId());
                supplyWarehouseGoodsDto.setGoodsSkuId(hxyGoodsSkuResDto.getGoodsSkuId());
                supplyWarehouseGoodsDto.setGoodsId(hxyGoodsSkuResDto.getGoodsId());
                supplyWarehouseGoodsDto.setWarehouseId(hxyGoodsSkuResDto.getWarehouseId());
                supplyWarehouseGoodsDto.setGoodsNum(hxyGoodsSkuResDto.getGoodsSkuOuterId());
                supplyWarehouseGoodsDto.setGoodsClass(hxyGoodsSkuResDto.getGoodsClass());
                supplyWarehouseGoodsDto.setGoodsSkuName(hxyGoodsSkuResDto.getGoodsName());
                supplyWarehouseGoodsDto.setGoodsSkuSubName(hxyGoodsSkuResDto.getGoodsSkuSubName());
                supplyWarehouseGoodsDto.setStock(hxyGoodsSkuResDto.getStock());
                supplyWarehouseGoodsDto.setAvailableStock(hxyGoodsSkuResDto.getAvailableStock());
                supplyWarehouseGoodsDto.setLockedStock(hxyGoodsSkuResDto.getLockedStock());
                supplyWarehouseGoodsDtoList.add(supplyWarehouseGoodsDto);
            }
        }

        return supplyWarehouseGoodsDtoList;
    }
    private HxyGoodsResultResDto getHxyWareGoodsSku() throws IOException {
        HttpClient httpClient = new DefaultHttpClient();
        String url = "https://kuaigongyuncang.huoxiaoyi.com/api/customer/goods/searchGoodsSkuWarehouseList?goodsClass=1&page=1&pageSize=500";
        HttpGet httpGet = new HttpGet(url);

        httpGet.addHeader("cookie", "_bl_uid=5Rlen8e14y1nnvdmhxswxz3ma5s5; tms_customer=e04k5b9j1dp1vhb9rvrs223q06; customerId=18972; accountType=platform; customerId=18972");
        HttpResponse response = httpClient.execute(httpGet);
        HttpEntity entity = response.getEntity();
        String responseStr = EntityUtils.toString(entity, "UTF-8");
        HxyGoodsResultResDto hxyGoodsResultResDto = new Gson().fromJson(responseStr, HxyGoodsResultResDto.class);
        return hxyGoodsResultResDto;
    }


}
